package leap;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.*;

public class LeapTest {
	/** position of quad */
	float x = 10, y = 10;
	/** angle of quad rotation */
	float rotation = 0;
	
	/** time at last frame */
	long lastFrame;
	
	/** frames per second */
	int fps;
	/** last fps time */
	long lastFPS;
	
	public void start() {
		try {
			Display.setDisplayMode(new DisplayMode(250,250));
			Display.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(0);
		}
		
		initGL(); // init OpenGL
		getDelta(); // call once before loop to initialise lastFrame
		lastFPS = getTime(); // call before loop to initialise fps timer
		
		
		int i = 0;
		boolean up = true;
		while (!Display.isCloseRequested()) {
			// render OpenGL here
			// Clear the screen and depth buffer
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);	
					
			// set the color of the quad (R,G,B,A)
			GL11.glColor3f(0.5f,0.5f,1.0f);
				
			// draw quad
			GL11.glBegin(GL11.GL_QUADS);
			    GL11.glVertex2f(10 + i,10 + i);
			    GL11.glVertex2f(10+30 + i,10 + i);
			    GL11.glVertex2f(10+30 + i,10+30 + i);
			    GL11.glVertex2f(10 + i,10+30 + i);
			GL11.glEnd();
			
			if(i>200)
				up = false;

			if(i<1)
				up = true;
			
			if(up)
				i++;
			else
				i--;
			
			
			pollInput();
			Display.update();
		}
		
		Display.destroy();
	}
	
	private void initGL() {
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, 250, 0, 250, 1, -1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		
	}

	public void pollInput() {
        if (Mouse.isButtonDown(0)) {			
		    System.out.println("MOUSE @ X:" + Mouse.getX() + " Y:" + Mouse.getY());
		}
			
		if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
		    System.out.println("SPACE KEY IS DOWN");
		}
			
		while (Keyboard.next()) {
		    if (Keyboard.getEventKeyState()) { //button pushed
		        if (Keyboard.getEventKey() == Keyboard.KEY_A) {
			    System.out.println("A Key Pressed");
				}
				if (Keyboard.getEventKey() == Keyboard.KEY_S) {
				    System.out.println("S Key Pressed");
				}
				if (Keyboard.getEventKey() == Keyboard.KEY_D) {
				    System.out.println("D Key Pressed");
				}
		    } else { //buton released
		        if (Keyboard.getEventKey() == Keyboard.KEY_A) {
		        	System.out.println("A Key Released");
		        }
		    	if (Keyboard.getEventKey() == Keyboard.KEY_S) {
		    		System.out.println("S Key Released");
				}
				if (Keyboard.getEventKey() == Keyboard.KEY_D) {
				    System.out.println("D Key Released");
				}
		    }
		}
	}

	public static void main(String[] args) {
		LeapTest displayExample = new LeapTest();
		displayExample.start();
	}
	
//	/**
//	 * Get the time in milliseconds
//	 * 
//	 * @return The system time in milliseconds
//	 */
//	public long getTime() {
//	    return System.nanoTime() / 1000000;
//	}
	/**
	 * Get the time in milliseconds
	 * 
	 * @return The system time in milliseconds
	 */
	public long getTime() {
	    return (Sys.getTime() * 1000) / Sys.getTimerResolution();
	}
	public int getDelta() {
	    long time = getTime();
	    int delta = (int) (time - lastFrame);
	    lastFrame = time;
	    	
	    return delta;
	}


	/**
	 * Calculate the FPS and set it in the title bar
	 */
	public void updateFPS() {
	    if (getTime() - lastFPS > 1000) {
	        Display.setTitle("FPS: " + fps); 
	        fps = 0; //reset the FPS counter
	        lastFPS += 1000; //add one second
	    }
	    fps++;
	}
}
